﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Refugees.Views.Windows
{
    /// <summary>
    /// Логика взаимодействия для NewsEdit.xaml
    /// </summary>
    public partial class NewsEdit : Window
    {
        Repositories.NewsRepository newsRepository = new Repositories.NewsRepository();
        Models.News news =new Models.News();
        public NewsEdit()
        {
            InitializeComponent();
            DataContext = news;
            newsRepository.AddNews(news);
        }

        private void BackClick(object sender, RoutedEventArgs e)
        {
            new News().Show();
            Close();
        }

        private void NewImage(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.DefaultExt = ".png";
            openFileDialog.Filter = "Изображения | *.png; *.jpg";
            openFileDialog.Multiselect = false;

            if (openFileDialog.ShowDialog() == true)
            {
                news.ImageNews = File.ReadAllBytes(openFileDialog.FileName);
                Image.Source = new BitmapImage(new Uri(openFileDialog.FileName));
            }
        }

        private void SaveClick(object sender, RoutedEventArgs e)
        {
            if (Repositories.ModelRepository.Save())
            {
                MessageBox.Show("Данные сохранены успешно");
            }
            else
            {
                MessageBox.Show("Данные не сохранены. Повторите попытку позже или проверьте данные");
            }
        }
    }
}
